import werobot
import datetime

robot = werobot.WeRoBot(token='kx8M7eODU4HiyAmw3a')
robot.config['APP_ID'] = 'wx47577a029b258f6b'
robot.config['ENCODING_AES_KEY'] = 'LjBrnwSvqGXx89Tszleo4sMYp8yMALcTArwhPjxoZGJ'

fake_chat_dict = {
    0: '十全十美！',
    1: '一把钢枪交给我！',
    2: '二话不说为祖国！',
    3: '三山五岳任我走。',
    4: '四海为家。',
    5: '五福同寿。',
    6: '六六大顺！',
    7: '97香港回归。',
    8: '零八奥运。',
    9: '九九归一。',
}


@robot.handler
def hello(message):
    
    xtype = message.type
    print(datetime.datetime.now(), xtype)
    if 'text' != xtype:
        return '目前仅支持文本信息！'
    
    xuserid = message.source
    print('userid:', type(xuserid), xuserid)
    
    xinput = message.content.strip()
    print('input:', xinput)
    xlen = len(xinput)
    xoutput = f'userid={xuserid}, 您说了{xlen}个字（{xinput[:5]}……），{fake_chat_dict[xlen % 10]}'
    print('output:', xoutput)
    return xoutput


# 让服务器监听在 0.0.0.0:80
robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()
